package parser;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class BaseParser {
	protected Connection connection;
	protected Connection connection2;
	protected List<Team> teamList = new ArrayList<Team>();
	protected List<Player> players;
	
	protected int preSeasonRound = -1;
	
	public void init() {
		establishConnection();
		populateTeams();
		populatePlayers();
	}
	
	protected boolean checkIfTeam(String line, boolean useDbName) {
		for (Team team : teamList) {
			if (useDbName) {
				if (team.getDbName().equals(line)) {
					return true;
				}
			}
			if (team.getName().equals(line)) {
				return true;
			}
		}
		
		return false;
	}
	
	protected String getMapKey(String name, boolean useShortName) {
		
		for (Iterator<Team> iter = teamList.iterator(); iter.hasNext();) {
			Team team = iter.next();
			if (useShortName) {
				for (int i = 0; i < team.getShortName().length; i++) {
					if (team.getShortName()[i].equals(name)) {
						return team.getDbName();						
					}
				}
			}
			else if (team.getName().equals(name)) {
				return team.getDbName();
			}
		}
		
		throw new RuntimeException("No team found for - " + name);
		
	}

	private void populateTeams() {
		teamList.add(new Team("ADELAIDE", new String[] {"ADEL", "ADL"}, "Adelaide"));
		teamList.add(new Team("BRISBANE", new String[] {"BRIS"}, "Brisbane"));
		teamList.add(new Team("CARLTON", new String[] {"CARL"}, "Carlton"));
		teamList.add(new Team("COLLINGWOOD", new String[] {"COLL", "COL"}, "Collingwood"));
		teamList.add(new Team("ESSENDON", new String[] {"ESS"}, "Essendon"));
		teamList.add(new Team("FREMANTLE", new String[] {"FREM", "FREO", "FRE"}, "Fremantle"));
		teamList.add(new Team("GC", new String[] {"GC"}, "Gold Coast"));
		teamList.add(new Team("GEELONG", new String[] {"GEEL", "GEL"}, "Geelong"));
		teamList.add(new Team("HAWTHORN", new String[] {"HAW", "HAWK"}, "Hawthorn"));
		teamList.add(new Team("MELBOURNE", new String[] {"MELB"}, "Melbourne"));
		teamList.add(new Team("NORTH_MELBOURNE", new String[] {"NTH", "KANG"}, "North Melbourne"));
		teamList.add(new Team("PORT_ADELAIDE", new String[] {"PORT", "PA"}, "Port Adelaide"));
		teamList.add(new Team("RICHMOND", new String[] {"RICH"}, "Richmond"));
		teamList.add(new Team("ST_KILDA", new String[] {"STK"}, "St Kilda"));
		teamList.add(new Team("SYDNEY", new String[] {"SYD"}, "Sydney"));
		teamList.add(new Team("WCE", new String[] {"WCE"}, "West Coast"));
		teamList.add(new Team("WESTERN_BULLDOGS", new String[] {"WBD", "DOGS"}, "Western Bulldogs"));
		teamList.add(new Team("GWS", new String[] {"GWS"}, "Greater Western Sydney"));
	}
	
	class Team {
		private String dbName;
		private String[] shortName;
		private String name;

		public Team(String dbName, String[] shortName, String name) {
			super();
			this.dbName = dbName;
			this.shortName = shortName;
			this.name = name;
		}
		public String getDbName() {
			return dbName;
		}
		public void setDbName(String dbName) {
			this.dbName = dbName;
		}
		public String[] getShortName() {
			return shortName;
		}
		public void setShortName(String[] shortName) {
			this.shortName = shortName;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
	}
	
	public void populatePlayers() {
		players = new ArrayList<Player>();
		players.add(new Player("Lachie", "Henderson", "Carlton", "Lachlan", "Henderson"));
		players.add(new Player("Chris", "Yarran", "Carlton", "Christopher", "Yarran"));
		players.add(new Player("Scott", "D. Thompson", "North Melbourne", "Scott", "Thompson"));
		players.add(new Player("Sam", "Wright", "North Melbourne", "Samuel", "Wright"));
		players.add(new Player("Josh", "P. Kennedy", "Sydney", "Josh", "Kennedy"));
		players.add(new Player("L.", "Roberts-Thomson", "Sydney", "Lewis", "Roberts-Thomson"));
		players.add(new Player("Matt", "Maguire", "Brisbane", "Matthew", "Maguire"));
		players.add(new Player("S.", "Prestigiacomo", "Collingwood", "Simon", "Prestigiacomo"));
		players.add(new Player("Ben", "Stratton", "Hawthorn", "Benjamin", "Stratton"));
		players.add(new Player("Jefferey", "Garlett", "Carlton", "Jeff", "Garlett"));
		players.add(new Player("Lachie", "Hansen", "North Melbourne", "Lachlan", "Hansen"));
		players.add(new Player("Scott D.", "Thompson", "North Melbourne", "Scott", "Thompson"));
		players.add(new Player("Josh P.", "Kennedy", "Sydney", "Josh", "Kennedy"));
		players.add(new Player("Josh J.", "Kennedy", "West Coast", "Joshua", "Kennedy"));
		players.add(new Player("Josh", "Kennedy", "West Coast", "Joshua", "Kennedy"));
		players.add(new Player("Nick", "Naitanui", "West Coast", "Nicholas", "Naitanui"));
		players.add(new Player("Nic", "Naitanui", "West Coast", "Nicholas", "Naitanui"));
		players.add(new Player("Matt", "Priddis", "West Coast", "Matthew", "Priddis"));
		players.add(new Player("Matt", "Rosa", "West Coast", "Matthew", "Rosa"));
		players.add(new Player("Matt", "Spangher", "West Coast", "Matthew", "Spangher"));
		players.add(new Player("Ricky", "Dyson", "Essendon", "Rick", "Dyson"));
		players.add(new Player("Matt", "White", "Richmond", "Matthew", "White"));
		players.add(new Player("Mitch", "Duncan", "Geelong", "Mitchell", "Duncan"));
		players.add(new Player("Robbie", "Gray", "Port Adelaide", "Robert", "Gray"));
		players.add(new Player("Chris", "Mayne", "Fremantle", "Christopher", "Mayne"));
		players.add(new Player("Mitch", "Clark", "Brisbane", "Mitchell", "Clark"));
		players.add(new Player("Nathan J.", "Brown", "Collingwood", "Nathan", "Brown"));
		players.add(new Player("Ben", "McKinley", "West Coast", "Benjamin", "McKinley"));
		players.add(new Player("Danny", "Hughes", "Melbourne", "Daniel", "Hughes"));
		players.add(new Player("Matt", "Suckling", "Hawthorn", "Matthew", "Suckling"));
		players.add(new Player("Chris", "Johnson", "Carlton", "Christopher", "Johnson"));
		players.add(new Player("Nick", "Suban", "Fremantle", "Nicholas", "Suban"));
		players.add(new Player("Zac", "Clarke", "Fremantle", "Zach", "Clarke"));
		players.add(new Player("Robbie", "Tarrant", "North Melbourne", "Robert", "Tarrant"));
		players.add(new Player("Andrew L.", "Browne", "Richmond", "Andrew", "Browne"));
		players.add(new Player("Benjamin", "Speight", "North Melbourne", "Ben", "Speight"));
		players.add(new Player("Brad", "Fisher", "Carlton", "Bradley", "Fisher"));
		players.add(new Player("Robbie", "Hicks", "Richmond", "Robert", "Hicks"));
		players.add(new Player("Jamie", "O'Rielly", "Richmond", "James", "O'Rielly"));
		players.add(new Player("Brad", "Moran", "Adelaide", "Bradley", "Moran"));
		players.add(new Player("Dan", "Nicholson", "Melbourne", "Daniel", "Nicholson"));
		players.add(new Player("Thomas", "Ledger", "St Kilda", "Tom", "Ledger"));
		players.add(new Player("Nick", "Winmar", "St Kilda", "Nicholas", "Winmar"));
		players.add(new Player("Joshua", "Green", "Brisbane", "Josh", "Green"));
		players.add(new Player("Thomas", "Nicholls", "Gold Coast", "Tom", "Nicholls"));
		players.add(new Player("Sebastian", "Tape", "Gold Coast", "Seb", "Tape"));
		players.add(new Player("Matthew", "Shaw", "Gold Coast", "Matt", "Shaw"));
		players.add(new Player("Ben", "McKinley", "North Melbourne", "Benjamin", "McKinley"));
		players.add(new Player("Edward", "Curnow", "Carlton", "Ed", "Curnow"));
		players.add(new Player("Bradley", "Helbig", "Richmond", "Brad", "Helbig"));
		players.add(new Player("Heritier", "O''Brien", "Collingwood", "Harry", "O''Brien"));
		players.add(new Player("Nick", "Duigan", "Carlton", "Nicholas", "Duigan"));
		players.add(new Player("Jeffrey", "Garlett", "Carlton", "Jeff", "Garlett"));
		players.add(new Player("Matthew", "de Boer", "Fremantle", "Matthew", "DeBoer"));
		players.add(new Player("Daniel", "Stanley", "Gold Coast", "Danny", "Stanley"));
		players.add(new Player("Mitch", "Brown", "West Coast", "Mitchell", "Brown"));
		players.add(new Player("Bradley", "Ebert", "West Coast", "Brad", "Ebert"));
		players.add(new Player("Tom", "L. Williams", "Western Bulldogs", "Tom", "Williams"));
		players.add(new Player("Thomas", "Liberatore", "Western Bulldogs", "Tom", "Liberatore"));
		players.add(new Player("Nicholas", "Smith", "Sydney", "Nick", "Smith"));
		players.add(new Player("Sebastian", "Tape", "Gold Coast", "Seb", "Tape"));
		players.add(new Player("Zachary", "Smith", "Gold Coast", "Zac", "Smith"));
		players.add(new Player("Josh", "Toy", "Gold Coast", "Joshua", "Toy"));
		players.add(new Player("Keiran", "Harper", "North Melbourne", "Kieran", "Harper"));
		players.add(new Player("Brad", "Sheppard", "West Coast", "Bradley", "Sheppard"));
		players.add(new Player("Aidan", "Riley", "Adelaide", "Aiden", "Riley"));
		players.add(new Player("Alexander", "Fasolo", "Collingwood", "Alex", "Fasolo"));
		players.add(new Player("Ben", "Bucovaz", "Fremantle", "Benjamin", "Bucovaz"));
		players.add(new Player("Niall", "McKeever", "Brisbane", "Nial", "McKeever"));
		players.add(new Player("Matthew", "Spangher", "Sydney", "Matt", "Spangher"));
		players.add(new Player("Ben", "Griffiths", "Richmond", "Benjamin", "Griffiths"));
		
	}
	
	class Player {
		private String firstName;
		private String surname;
		private String team;
		private String requiredFirstName;
		private String requiredSurname;
		
		public Player(String firstName, String surname, String team, String requiredFirstName, String requiredSurname) {
			super();
			this.firstName = firstName;
			this.surname = surname;
			this.team = team;
			this.requiredFirstName = requiredFirstName;
			this.requiredSurname = requiredSurname;
		}

		public String getFirstName() {
			return firstName;
		}
		
		public void setFirstName(String firstName) {
			this.firstName = firstName;
		}
		
		public String getSurname() {
			return surname;
		}
		
		public void setSurname(String surname) {
			this.surname = surname;
		}
		
		public String getTeam() {
			return team;
		}
		
		public void setTeam(String team) {
			this.team = team;
		}

		public String getRequiredFirstName() {
			return requiredFirstName;
		}

		public void setRequiredFirstName(String requiredFirstName) {
			this.requiredFirstName = requiredFirstName;
		}

		public String getRequiredSurname() {
			return requiredSurname;
		}

		public void setRequiredSurname(String requiredSurname) {
			this.requiredSurname = requiredSurname;
		}
		
	}
	
	private void establishConnection() {
		try {
			Class.forName( "com.mysql.jdbc.Driver" );
			connection = DriverManager.getConnection(
				     "jdbc:mysql://localhost/sffdraftgame",
				     "root",
				     "password" );

		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void establishConnection2() {
		try {
			Class.forName( "com.mysql.jdbc.Driver" );
			connection2 = DriverManager.getConnection(
				     "jdbc:mysql://localhost/sff_2009",
				     "root",
				     "password" );

		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}	
	
	protected int getRound() throws SQLException {
		if (preSeasonRound > 0) {
			return preSeasonRound + 50;
		}
		
		return getUniqueInteger("select min(round) from Round where complete = false");
	}
	
	protected int getUniqueInteger(String sql) {
		Statement stmt = null;
		ResultSet resultSet = null;
		int result = -1;
		try {
			stmt = connection.createStatement();
			resultSet = stmt.executeQuery(sql);
			resultSet.next();
			result = resultSet.getInt(1);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			try {
				stmt.close();
				resultSet.close();
			}
			catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		
		return result;
	}
	
	protected Player getPlayer(String team, String firstName, String surname) {
		Player reqPlayer = null;
		for (Player player : players) {
			if (player.getFirstName().equals(firstName) && player.getSurname().equals(surname)) {
				if (team == null || getMapKey(player.getTeam(), false).equals(team)) {
					reqPlayer = player;
					
					break;
				}
			}
		}
		
		return reqPlayer;
	}
	
}
